import pygtk
import gtk
import gtk.gdk as gdk
import xml.etree.ElementTree as et
import geometry

# *********************************************************************
class HTMLLayout(gtk.ScrolledWindow):
	
	# -------------------------------------------------------------------
	def __init__(self, xml = None):
		gtk.ScrolledWindow.__init__(self)
		
		self.layout = gtk.Layout()
		self.widgets	=	{}
		
		self.add(self.layout)
		
		if xml:
			self.root	=	et.parse(xml).getroot()
		else:
			self.root	=	None
			
		self.layout.connect("expose_event", self.OnExpose)
		
	# -------------------------------------------------------------------
	def OnExpose(self, widget, event):
		wnd	=	widget.bin_window
		
		dw, dh = wnd.get_size()
		
		if dw < 0 or dh < 0:
			return
		
		ctx = wnd.cairo_create()
		
		ctx.rectangle( (0, 0, dw, dh) )
		ctx.clip_preserve()
		
		ctx.set_source_rgb(1, 1, 1)
		ctx.rectangle( (0, 0, dw, dh) )
		ctx.fill()
		
		x, y = 0, 0
		rects = []
		
		if self.root:
			for e in self.root:
				if e.tag == 'p':
					layout = ctx.create_layout()
					layout.set_text(self.text)
					layout.set_alignment(pango.ALIGN_LEFT)
					layout.set_font_description( pango.FontDescription('sans bold %s' % 12) )
					layout.set_width(dw - x)
					layout.show_layout()
					lw, lh = layout.get_pixel_size()
					y += lh
					
		
	# -------------------------------------------------------------------
	def Parse(self, xml):
		self.root = et.parse(xml).getroot()
	
	
